<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/3
 * Time: 17:51
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Recycleinfo extends Base
{
    /**
     * 故障小项列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $ri_brand_id = input('ri_brand_id');
            $ri_models_id = input('ri_models_id');
            $ri_recycle_id = input('ri_recycle_id');
            $ri_name = trim(input('ri_name'));
            
            $where['ri_status'] = 1;
            if(isset($ri_name) && $ri_name != ''){
                $where['ri_name'] = ['like', '%' . $ri_name . '%'];
            }
            if(isset($ri_brand_id) && $ri_brand_id != ''){
                $where['ri_brand_id'] = $ri_brand_id;
            }
            if(isset($ri_models_id) && $ri_models_id != ''){
                $where['ri_models_id'] = $ri_models_id;
            }
            if(isset($ri_recycle_id) && $ri_recycle_id != ''){
                $where['ri_recycle_id'] = $ri_recycle_id;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('recycle_info')
                ->join('up_brand', 'brand_id = ri_brand_id', 'LEFT')
                ->join('up_models', 'models_id = ri_models_id', 'LEFT')
                ->join('up_recycle', 'ri_recycle_id = recycle_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('recycle_info')
                ->join('up_brand', 'brand_id = ri_brand_id', 'LEFT')
                ->join('up_models', 'models_id = ri_models_id', 'LEFT')
                ->join('up_recycle', 'ri_recycle_id = recycle_id', 'LEFT')
                ->field('ri_id, brand_name, models_name, recycle_name, ri_price, ri_is_hb, ri_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ri_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $this->assign('brand', $brand);
        return $this->fetch();
    }
    
    /**
     * 添加回收故障小项
     * @return mixed|\think\response\Json
     */
    public function infoAdd(){
        if(Request::instance()->isPost()){
            $ri_brand_id = input('ri_brand_id');
            $ri_models_id = input('ri_models_id');
            $ri_recycle_id = input('ri_recycle_id');
            $ri_name = trim(input('ri_name'));
            $ri_price = input('ri_price');
            $ri_is_hb = input('ri_is_hb');
            $ri_images = input('photo');
            $ri_word = input('ri_word');
            
            $insert = [
                'ri_brand_id' => $ri_brand_id,
                'ri_models_id' => $ri_models_id,
                'ri_recycle_id' => $ri_recycle_id,
                'ri_name' => $ri_name,
                'ri_price' => $ri_price,
                'ri_is_hb' => $ri_is_hb,
                'ri_images' => $ri_images,
                'ri_word' => $ri_word,
                'ri_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_recycle_info')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $this->assign('brand', $brand);
        return $this->fetch();
    }
    
    /**
     * 编辑回收故障小项
     * @return mixed|\think\response\Json
     */
    public function infoEdit(){
        $ri_id = input('ri_id');
        if(Request::instance()->isPost()){
            $ri_brand_id = input('ri_brand_id');
            $ri_models_id = input('ri_models_id');
            $ri_recycle_id = input('ri_recycle_id');
            $ri_name = trim(input('ri_name'));
            $ri_price = input('ri_price');
            $ri_is_hb = input('ri_is_hb');
            $ri_images = input('photo');
            $ri_word = input('ri_word');
    
            $update = [
                'ri_id' => $ri_id,
                'ri_brand_id' => $ri_brand_id,
                'ri_models_id' => $ri_models_id,
                'ri_recycle_id' => $ri_recycle_id,
                'ri_name' => $ri_name,
                'ri_price' => $ri_price,
                'ri_is_hb' => $ri_is_hb,
                'ri_images' => $ri_images,
                'ri_word' => $ri_word,
                'ri_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_recycle_info')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_recycle_info')
            ->where('ri_id', $ri_id)
            ->field('ri_id, ri_brand_id, ri_models_id, ri_recycle_id, ri_price, ri_is_hb, ri_images, 
            ri_word, ri_name')
            ->find();
        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $models = Db::table('up_models')
            ->where(['models_status' => 1, 'models_brand_id' => $info['ri_brand_id']])
            ->field('models_id, models_name')
            ->select();
        $recycle = Db::table('up_recycle')
            ->where(['recycle_status' => 1, 'recycle_models_id' => $info['ri_models_id']])
            ->field('recycle_id, recycle_name')
            ->select();
        $this->assign([
            'info' => $info,
            'brand' => $brand,
            'models' => $models,
            'recycle' => $recycle,
            'ri_id' => $ri_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function infoDetail(){
        $ri_id = input('ri_id');
        $info = Db::table('up_recycle_info')
            ->join('up_brand', 'brand_id = ri_brand_id', 'LEFT')
            ->join('up_models', 'models_id = ri_models_id', 'LEFT')
            ->join('up_recycle', 'ri_recycle_id = recycle_id', 'LEFT')
            ->where('ri_id', $ri_id)
            ->field('ri_name, brand_name, models_name, recycle_name, ri_price, ri_is_hb, ri_images, 
            ri_word, ri_time')
            ->find();
        if($info['ri_images']){
            $info['ri_images'] = explode(',', $info['ri_images']);
        }else{
            $info['ri_images'] = [];
        }
        $this->assign('info', $info);
        return $this->fetch();
            
    }
    
    /**
     * 删除回收故障小项
     * @return \think\response\Json
     */
    public function infoDel(){
        $ri_id = input('id');
        $res = Db::table('up_recycle_info')->where('ri_id', $ri_id)->setField('ri_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}